const { connection } = require('./index');
const { isAccountHasPortrait } = require('./account');
const { server } = require('../config/config.default');

const getReplyListOfComment = (commentId) => {
  return new Promise((resolve, reject) => {
    if (!commentId) {
      reject({ message: 'Reply is not found in your request.' });
    }
    connection.query(
      `SELECT * FROM reply WHERE comment_id='${commentId}'`,
      (err, result) => {
        if (err) {
          reject({ message: 'Reply is not found in your request.' });
        }
        result.map((item) => {
          isAccountHasPortrait(item.from_user).then(
            (r) => {
              if (r[0].has_portrait === '0') {
                item.user_ptofile = server.baseURL + '/portrait/' + 'default.png'
              } else {
                item.user_ptofile = server.baseURL + '/portrait/' + r[0].has_portrait
              }
            },
            (e) => {
              reject({ message: 'User_profile is not found in your request.' });
            }
          )
        })
        resolve(result);
      }
    );
  });
}

const getReplyById = (replyId) => {
  return new Promise((resolve, reject) => {
    if (!replyId) {
      reject({ message: 'Reply is not found in your request.' });
    }
    connection.query(
      `SELECT * FROM reply WHERE reply_id='${replyId}'`,
      (err, result) => {
        if (err) {
          reject({ message: 'Reply is not found in your request.' });
        }
        console.log(result);
        resolve(result);
      }
    );
  });
}

const addReply = (info) => {
  const { comment_id, to_reply_id, from_user, to_user, reply_text, reply_likes } = info;
  const now_time = new Date();
  const sqlParams = [comment_id, now_time, to_reply_id, from_user, to_user, reply_text, reply_likes];

  return new Promise((resolve, reject) => {
    connection.query(
      'INSERT INTO reply(comment_id, create_time, to_reply_id, from_user, to_user, reply_text, reply_likes) VALUES(?,?,?,?,?,?,?)',
      sqlParams,
      (err, result) => {
        if (err) {
          reject({ err, message: 'Failed to add.' });
        }
        resolve(result);
      }
    );
  });
}

const likeReply = (replyId) => {
  return new Promise((resolve, reject) => {
    if (!replyId) {
      reject({ message: 'Reply is not found in your request.' });
    }
    connection.query(
      `UPDATE reply set reply_likes = reply_likes+1 WHERE reply_id='${replyId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Reply is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
}

const unlikeReply = (replyId) => {
  return new Promise((resolve, reject) => {
    if (!replyId) {
      reject({ message: 'Reply is not found in your request.' });
    }
    connection.query(
      `UPDATE reply set reply_likes = reply_likes - 1 WHERE reply_id='${replyId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Reply is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
}

const deleteReply = (replyId) => {
  return new Promise((resolve, reject) => {
    if (!replyId) {
      reject({ message: 'Reply is not found in your request.' });
    }
    connection.query(
      `DELETE FROM reply where reply_id='${replyId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Reply is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
}

module.exports = {
  getReplyListOfComment,
  getReplyById,
  addReply,
  likeReply,
  unlikeReply,
  deleteReply
}
